import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

class Adress implements Serializable {
	
	public String adr;

	public Adress(String a){
		
		adr=a;
	}
}

class Account implements Serializable {
	
	public String acc;
	
	public Account(String a){
		
		acc=a;
	}
}

class Person implements Serializable {
	
	public String first;
	public String last;
	public Adress adressInfo;
	public Account accountInfo;
	
	public Person(String f, String l, String ad, String ac){
		
		first=f;
		last=f;
		adressInfo=new Adress(ad);
		accountInfo=new Account(ac);
		
	}
	
	public static void serializePerson(Person p,
			String fileName) throws IOException {
			FileOutputStream fileOut =
			new FileOutputStream(fileName);
			ObjectOutputStream objOut =
			new ObjectOutputStream(fileOut);
			try {
			objOut.writeObject(p);
			} finally {
			objOut.close();
			fileOut.close();
			}
			}

	public static Person deserializePerson(String fileName)
	throws IOException, ClassNotFoundException {
	FileInputStream fileIn =
	new FileInputStream(fileName);
	ObjectInputStream objIn =
	new ObjectInputStream(fileIn);
	try {
	Person p = (Person) objIn.readObject();
	return p;
	}
	finally {
	objIn.close();
	fileIn.close();
	}
	}	
	
}

public class e01 {

	/**
	 * @param args
	 * @throws IOException 
	 * @throws ClassNotFoundException 
	 */
	public static void main(String[] args) throws IOException, ClassNotFoundException {
		// TODO Auto-generated method stub

		Person p= new Person("Nik","Kalchev","ul. Lipa 3","123");
		
		p.serializePerson(p, "c:\\gvb\\person.bin");
		
		p.first="Georgi";
		
		p=p.deserializePerson("c:\\gvb\\person.bin");
		
		System.out.println(p.first);
	}

}
